package net.itgoo.parkingpay.ui.rfid;

import net.itgoo.parkingpay.R;
import net.itgoo.parkingpay.manage.ParkingManage;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import java.util.Map;

/**
 * Created by apple on 16/11/21.
 */
public class ParkingRfidPresenter implements ParkingRfidContract.Presenter {

    private final ParkingRfidContract.View mView;
    private final ParkingRfidContract.DataSource mRepository;
    private Map<String, String> mParam;

    public ParkingRfidPresenter(Map<String, String> param, ParkingRfidContract.View view,
                                ParkingRfidContract.DataSource repository) {
        mView = view;
        mRepository = repository;
        mParam = param;
        view.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {
        mRepository.cancel();
    }

    @Override
    public void getData() {
        if (mParam.containsKey("card_id") && Integer.valueOf(mParam.get("card_auto")) == 1) {
            mView.showBindInfo(mParam.get("plate"), mParam.get("card_number"),
                    Integer.valueOf(mParam.get("card_auto")) == 1 ? true : false);
        }
    }

    @Override
    public void bind(String phone, final String tag, String code) {
        if (phone.isEmpty()) {
            mView.showBindFailure(mView.getContext().getString(R.string.parking_mobile_empty_error));
            return;
        }

        if (tag.isEmpty()) {
            mView.showBindFailure(mView.getContext().getString(R.string.parking_tag_empty_error));
            return;
        }

        if (code.isEmpty()) {
            mView.showBindFailure(mView.getContext().getString(R.string.parking_opt_empty_error));
            return;
        }

        String userId = ParkingManage.getInstance(mView.getContext()).getUserId();
        mRepository.bind(userId, mParam.get("plate"), phone, tag, code, new ParkingMVPLoadDataCallback<String>() {
            @Override
            public void onDataLoaded(String data) {
                if (!mView.isActive()) {
                    return;
                }

                mParam.put("card_id", data);
                mParam.put("card_number", tag);
                mParam.put("card_auto", "0");

                mView.showBindSuccess();
            }

            @Override
            public void onDataError(String error) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showBindFailure(error);
            }
        });

        mView.showLoading();
    }

    @Override
    public void auto(boolean open) {
        if (!mParam.containsKey("card_id")) {
            return;
        }

        mRepository.auto(mParam.get("card_id"), open ? 1 : 0, new ParkingMVPLoadDataCallback<String>() {
            @Override
            public void onDataLoaded(String data) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showAutoSuccess();
            }

            @Override
            public void onDataError(String error) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showAutoFailure(error);
            }
        });
    }
}
